//
//  SQSwiftSegment.swift
//  SQSwiftSegument
//
//  Created by taojianz on 2017/8/8.
//  Copyright © 2017年 taojianz. All rights reserved.
//

import UIKit

class SQSwiftSegment: UIView {

    // btn基础 tag
    private let baseBtnTag = 9999
    
    // 视图中所有按钮
    private var buttons : [UIButton]!
    
    // 文字颜色、字体
    var textNormalColor : UIColor = UIColor.black{
        didSet{
            for btn in buttons {
                btn.setTitleColor(textNormalColor, for: .normal)
            }
        }
    }
    var textHighLightColor : UIColor = UIColor.black{
        didSet{
            for btn in buttons {
                btn.setTitleColor(textHighLightColor, for: .highlighted)
            }
        }
    }
    
    var font : UIFont = UIFont.systemFont(ofSize: 13){
        didSet{
            for btn in buttons {
                btn.titleLabel?.font = font
            }
        }
    }
    // 视图边框颜色、粗细
    var borderWidth : CGFloat = 1{
        didSet{
            layer.borderWidth = borderWidth
        }
    }
    
    var borderColor : CGColor = UIColor.white.cgColor{
        didSet{
            layer.borderColor = borderColor
        }
    }
    // 视图的圆角
    var cornerRaidus : CGFloat!{
        didSet{
            layer.cornerRadius = cornerRaidus
            clipsToBounds  = true
        }
    }
    
    // 滑块
    private var slider : UIView!
    // 滑块的圆角
    var sliderCornerRadius : CGFloat!{
        didSet{
            slider.layer.cornerRadius = cornerRaidus
            clipsToBounds  = true
        }
    }
    // 滑块的颜色
    var sliderColor : UIColor = .white{
        didSet{
            slider.backgroundColor = sliderColor
        }
    }
    // 当前选中按钮索引
    var selectedBtnIndex : Int = 0{
        didSet{
            // 当前选中索引改变，则需要更新上一次选中按钮的 index
            lastSelectedBtnIndex = oldValue
            UIView.animate(withDuration: 0.3) { [unowned self] in
                self.slider.frame.origin.x = CGFloat(self.selectedBtnIndex) * self.slider.bounds.width
            }
        }
    }
    // 上一个按钮的索引
    var lastSelectedBtnIndex : Int = 0
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        // 设置上一个按钮索引
        
        
        // 设置边框
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor
        // 裁边
        layer.cornerRadius = frame.height/2
        clipsToBounds = true
        
    }
    
    convenience init(_ frame : CGRect , With titles:Array<String>){
        self.init(frame: frame)
        
        
        let count = titles.count
        
        let btnWidth = frame.width/CGFloat(count)
        let btnHeight = frame.height
        
        slider = UIView(frame: CGRect(x:CGFloat(selectedBtnIndex)*btnWidth, y: 0, width: btnWidth, height: btnHeight))
        
        slider.backgroundColor = sliderColor
        slider.layer.cornerRadius = slider.bounds.height/2
        
        addSubview(slider)
        
        buttons = Array()
        
        for i in 0..<count {
            let btn = UIButton(frame: CGRect(x: CGFloat(i)*btnWidth, y: 0, width: btnWidth, height: btnHeight))
            btn.titleLabel?.font = font
            btn.tag = baseBtnTag + i
            btn.setTitleColor(textNormalColor, for: .normal)
            btn.setTitleColor(textHighLightColor,for:.highlighted)
            btn.setTitle(titles[i], for: .normal)
            btn.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)
            addSubview(btn)
            buttons.append(btn)
        }
    }
    
    @objc private func btnTapped(_ button:UIButton){
        let btnIndex = button.tag - baseBtnTag
        selectedBtnIndex = btnIndex
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
